<?php
include_once('filtro_cabecera.php');
include_once('menu_operacion.php');
include_once "../funciones/funciones_php.php";
?>
<html>
<head>
<title>Asignar chofer retiro</title>
  <link rel="stylesheet" href="../css/plantilla.css" type="text/css" media="screen">
  <link rel="stylesheet" type="text/css" href="../css/plantilla.css" />
  <link rel="stylesheet" type="text/css" href="../css/tabla.css" />

<script>
        $(document).ready(function()
        {
            $('#buscar').button();
            $('#guardar').button();
        });

</script>
</head>
<body>
<br>
<div id="titulo_seccion">
    <fieldset>
        <table>
        <tr>
            <td><font>Asignar Chofer de retiro</font></td>
        </tr>
        </table>
    </fieldset>
</div>

<fieldset>
<legend>Buscar orden de flete</legend>
    <form action="asignar_chofer_retiro.php" name="formulario" method="post">
        <input type="text" name="orden_flete" />
        <input type="submit" name="buscar" id="buscar" value="Buscar" />
    </form>
</fieldset>

<?php
$orden = trim($_REQUEST['orden_flete']);

if($orden!=NULL)
{
    conectar();
    $pg = sprintf("SELECT *
                   FROM orden_flete,empleado
                   WHERE orden_flete.id_orden_flete = '%d'
                   AND empleado.nombre = '%s'
                   AND orden_flete.id_empleado_retiro = empleado.id_empleado",pg_escape_string($orden),pg_escape_string('Desconocido'));
    $conectar = pg_query($pg) or die ("ERROR");
    $filas = pg_num_rows($conectar);

        if($filas > 0)
        {
            while($reg = pg_fetch_array($conectar))
            {
                $orden_flete = $reg['id_orden_flete'];
            }

            echo "<fieldset>
                    <legend>Asignar Chofer</legend>";
            echo "Orden de flete: ".$orden_flete;
            ?>
            <form action="guardar_asignar_chofer.php" method="post">
            <?php
            echo "Chofer: ";

            $pg_c = sprintf("SELECT *
                            FROM empleado
                            WHERE empleado.id_tipo_empleado = '%d'",pg_escape_string(3));
            $cons_chof = pg_query($pg_c) or die ("ERROR");

            echo "<select name='empleado'>";

                while ($reg2 = pg_fetch_array($cons_chof))
                {
                echo "<option value=".$reg2['id_empleado'].">".$reg2['nombre']."";
                }

            echo "</select>";
            ?>
            <br><br />
            <input type="hidden" value="<?php echo $orden_flete ?>" name="orden">
            <input type="submit" value="Guardar" id="guardar"/>
            </form>
            <?php
            echo "</fieldset>";
        }
        else
        {
            echo "<fieldset>
                  <legend>Aviso</legend>
                    No se encontraron resultados para el numero de orden ingresado
                  </fieldset>";
        }
}
else
{
    conectar();
    $pg = sprintf("SELECT orden_flete.id_orden_flete, empleado.nombre
                   FROM orden_flete,empleado,detalle_encomienda,encomienda
                   WHERE orden_flete.id_empleado_retiro = empleado.id_empleado
                   AND empleado.nombre = '%s'
                   AND detalle_encomienda.id_encomienda = encomienda.id_encomienda
                   AND encomienda.id_orden_flete = orden_flete.id_orden_flete
                   GROUP BY orden_flete.id_orden_flete, empleado.nombre",pg_escape_string('Desconocido'));
    $conectar = pg_query($pg) or die ("Error en la consulta");

    $filas = pg_num_rows($conectar);

    if($filas > 0)
    {
    echo "<fieldset>
            <legend>Ordenes de Flete</legend>

                <table cellspacing=0 border=0 class=data>
                    <thead class=data>
                    <tr class=data>
                        <th>Orden de flete</th>
                        <th>Chofer</th>
                        <th>Cambiar Chofer</th>
                            </tr>
                            </thead>";
                            $i = 0;
                            while($reg = pg_fetch_array($conectar))
                            {
                                ($i % 2 == 0 )? $clase = " " : $clase = "class=odd";
                                echo "<tbody class=data>
                                <tr " . $clase . ">";

                                echo "<td>".$reg['id_orden_flete']."</td>";
                                echo "<td>".$reg['nombre']."</td>";
                                echo "<td><a href=javascript:Abrir_ventana('panel_cambiar_chofer.php?o_flete=".$reg['id_orden_flete']."')>Cambiar chofer</a></td>";
                                echo "</tr>";
                                $i++;
                            }
    echo "</table>";
    echo "</fieldset>";
    }
}
?>

</body>
</html>
